package com.atguigu.gmall.product.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.extension.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.PushBuilder;
import java.util.List;
@RefreshScope //修改配置 不用重启
//@CrossOrigin
@RestController
@RequestMapping("/admin/product")
public class ManageController {
    @Autowired
    private ManageService manageService;

    //查询所有的一级分类数据/getCategory1
    @GetMapping("/getCategory1")
    public Result GetCategory1() {
        //调用服务层方法
        List<BaseCategory1> baseCategory1List = manageService.GetCategory1();
        //返回数据
        return Result.ok(baseCategory1List);
    }

    //根据一级分类id查询所有的二级分类数据/getCategory2/{category1Id}
    //@PathVariable 获取url路径上的参数
    @GetMapping("getCategory2/{category1Id}")
    public Result GetCategory2(@PathVariable Long category1Id) {
        List<BaseCategory2> baseCategory2List = manageService.GetCategory2(category1Id);
        return Result.ok(baseCategory2List);
    }

    //根据二级分类id查询三级分类数据/getCategory3/{category2Id}
    @GetMapping("/getCategory3/{category2Id}")
    public Result GetCategory3(@PathVariable Long category2Id) {
        //调用服务层方法
        List<BaseCategory3> baseCategory3List = manageService.GetCategory3(category2Id);
        return Result.ok(baseCategory3List);
    }

    //根据分类id获取平台属性集合/attrInfoList/{category1Id}/{category2Id}/{category3Id}
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(@PathVariable Long category1Id,
                               @PathVariable Long category2Id,
                               @PathVariable Long category3Id) {
        //调用服务层方法
        List<BaseAttrInfo> baseAttrInfoList = manageService.getAttrInfoList(category1Id, category2Id, category3Id);
        return Result.ok(baseAttrInfoList);
    }
    //保存平台属性/admin/product/saveAttrInfo
    @PostMapping("/saveAttrInfo")
    //前端数据都封装在BaseAttrInfo中
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        //调用服务层方法
        manageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }
//修改平台属性值/getAttrValueList/{attrId}
    @GetMapping("/getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable Long attrId){

        //调用服务层方法
        //select *from baseAttrInfo where id =? 这个sql的含义是先判断平台属性有无，如果有平台属性在考虑回显平台属性值
        BaseAttrInfo baseAttrInfo=manageService.getBaseAttrInfo(attrId);
        return Result.ok(baseAttrInfo.getAttrValueList());

        //List<BaseAttrValue> baseAttrValueList=manageService.getAttrValueList(attrId);
        //return Result.ok(baseAttrValueList);
    }

}
